package com.niit.controller;

import com.niit.entity.Job;
import com.niit.entity.User;
import com.niit.repository.UserRepository;
import com.niit.service.JobService;
import com.niit.utils.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/*
 * 职位管理接口
 */

@RestController
@RequestMapping("/api/job")
public class JobController {
    @Autowired
    JobService jobService;
    @Autowired
    private UserRepository userRepository;

    //发布职位
    @PostMapping("/add")
    public void addJob(@RequestBody Job job, HttpSession session) {
        User user = (User) session.getAttribute("account");
        job.setUser(user);
        job.setPostTime(LocalDateTime.now());
        jobService.addJob(job);
    }

    //修改职位信息
    @PutMapping("/update")
    public void updateJob(@RequestBody Job job, HttpSession session) {
        User user = (User) session.getAttribute("account");
        job.setUser(user);
        // 设置更新时间(每次更新都列为重新发布)  后面可调整逻辑为（修改则同步,不修改则不变）
        job.setPostTime(LocalDateTime.now());
        job.setStatus((byte) 0);//修改后需要重新审核
        jobService.updateJob(job);
    }

    //删除职位信息
    @DeleteMapping("/delete/{id}")
    public void deleteJob(@PathVariable("id") Integer id) {
        jobService.deleteJob(id);
    }

    //获取职位信息
    @GetMapping("/get/{id}")
    public Job getJob(@PathVariable("id") Integer id) {
        return jobService.getJob(id);
    }

    //获取所有职位信息
    @GetMapping("/getAll")
    public List<Job> getAllJobs() {
        return jobService.getAllJobs();
    }

    //获取个人发布的职位信息
    @PostMapping("/getJobs")
    public List<Job> getMyJobs(@RequestBody User user) {
        return jobService.getMyJobs(user);
    }

    //获取个人发布的职位数量
    @GetMapping("/getJobCount")
    public Integer getJobCount(@RequestBody User user) {
        return jobService.getJobCount(user);
    }

    //搜索职位信息
    @GetMapping("/search")
    public List<Job> searchJob(String keyword) {
        return jobService.searchJob(keyword);
    }

    @GetMapping("/page")
    public Page<Job> getJobsByCondition(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "publishUser", required = false) String publishUser,
            @RequestParam(value = "status", required = false) Byte status,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit) {

            Pageable pageable = PageRequest.of(page - 1, limit);
            Page<Job> jobs = jobService.getJobsByCondition(title, publishUser, status, pageable);

            // 转换为前端需要的格式
            return jobs;
    }

    @GetMapping("/auditedJobs")
    public List<Job> getAuditedJobs() {
        return jobService.getAuditedJobs();
    }

    @GetMapping("/condition")
    public Page<Job>  searchJobByCondition(String location, String salaryRange, String keyword, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return jobService.findJobsByCondition(location, salaryRange, keyword, pageable);
    }


    //职位审核
    @PutMapping("/audit")
    public void auditJob(@RequestParam("id") Integer id, @RequestParam("status") Byte status) {
        jobService.auditJob(id, status);
    }

}
